#pragma once
#include <cstdint>
#include <unordered_map>
#include "EventEngine.h"
#include "UserManager.h"
using namespace xncc::types;

class ChatEngine : public xncc::user::EventEngine
{
  public:
    ChatEngine(int id = 10);
    ~ChatEngine() override = default;
    void init() override;
    void handleConnected(connection_id_t id, thread_id_t tid) override;
    void handleDisconnected(connection_id_t id, thread_id_t tid, const std::string& disconnectedReason) override;
    void closeClient(thread_id_t tid, connection_id_t connId);

    void handleUserLoginReq(const char* buf, uint32_t len, thread_id_t tid, connection_id_t connId);
    void handleUserLogoutReq(const char* buf, uint32_t len, thread_id_t tid, connection_id_t connId);
    void handleUserListReq(const char* buf, uint32_t len, thread_id_t tid, connection_id_t connId);
    void handleUserChatMsg(const char* buf, uint32_t len, thread_id_t tid, connection_id_t connId);
    void handleUserRegisterReq(const char* buf, uint32_t len, thread_id_t tid, connection_id_t connId);
    void handleAddDelFriendReq(const char* buf, uint32_t len, thread_id_t tid, connection_id_t connId);

  private:
    void broadcastFriendsOnOffline(user_id_t id, bool online);

  private:
    std::unordered_map<connection_id_t, bool> conn_ids_;
    UserManager                               user_manager_;
};